package com.jjc.android.activity

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.google.gson.reflect.TypeToken
import com.jjc.android.Constants
import com.jjc.android.R
import com.jjc.android.base.BaseActivity
import com.jjc.android.dialog.MessageImageDialog
import com.jjc.android.entity.*
import com.jjc.android.net.SimpleHttpCallback
import com.jjc.android.util.Common.parseEmpty
import com.jjc.android.util.Convert
import com.jjc.android.util.MultiLanguageUtil
import com.jjc.android.util.messenger.ActivityMessenger
import com.jjc.android.util.paramEx
import com.jjc.android.util.showImage
import com.jjc.android.view.layout.*
import com.jjc.android.view.onClick
import com.lzy.okgo.OkGo

class SamplingActivity : BaseActivity() {

    companion object{
        fun start(context: Activity, action:(Boolean)->Unit){
            val intent = Intent(context,SamplingActivity::class.java)
            ActivityMessenger.startActivityForResult(context,intent){
                (it?.getBooleanExtra(Constants.BUNDLE_INFO,false)==true).let(action)
            }
        }
    }

    override val layoutId: Int = R.layout.activity_sampling
    private val okBtn: TextView by lazy { findViewById<TextView>(R.id.okBtn) }
    private val homeBtn: View by lazy { findViewById(R.id.homeBtn) }
    private val samplingImageLayout:LinearLayout by lazy { findViewById(R.id.samplingImageLayout) }
    private val contentText:TextView by lazy { findViewById(R.id.contentText) }
    override fun initView(savedInstanceState: Bundle?) {
        setActivityTitle(getString(R.string.sampling_2_txt))
        okBtn.onClick {

            setResult(Activity.RESULT_OK, Intent().apply {
                putExtra(Constants.BUNDLE_INFO,true)
            })
            finish()
        }
        homeBtn.visibility = View.VISIBLE
        homeBtn.onClick {
            MessageImageDialog.Builder(this)
                .imageResId(R.mipmap.alert_jiancejindu_pic)
                .content(getString(R.string.dete_not_save_txt))
                .okClick {
                    MainActivity.start(this)
                }
                .build()
                .show()
        }
        fetchPageInfo()
    }


    override fun fetchPageInfo() {
        OkGo.post<LzyResponse<PageInfo<SamplingPageDto>>>(Constants.getFormList)
            .paramEx(mutableMapOf("page" to 1,"pagesize" to 1,"needpage" to 1,"formheaduuid" to "68b6063091a54242a0d3b7794ef14529","filters" to listOf<KeyValuePair>(
                KeyValuePair("vydvtklk0xjgsinm", MultiLanguageUtil.getInstance().getValue("746f5cd0-dee7-4a10-8f91-75b27ac123b4","69bd0b96-e704-4522-8543-4c140691793c")),
                KeyValuePair("ktrpe1fzjzsdoek8","58cb663ec25e44e2be710bdf246957b4")
            )),true)
            .tag(this)
            .execute(object : SimpleHttpCallback<LzyResponse<PageInfo<SamplingPageDto>>>(){
                override fun onCallSuccess(response: LzyResponse<PageInfo<SamplingPageDto>>) {
                    response.data?.first?.jsonMember9rjnk9bcnstxhdli.let { images->
                        if(images.isNullOrEmpty()){
                            samplingImageLayout.visibility = View.GONE
                        }else{
                            samplingImageLayout.visibility = View.VISIBLE
                            samplingImageLayout.removeAllViews()
                            images.forEach { image->
                                samplingImageLayout.ImageView {
                                    layout_width = match_parent
                                    layout_height = wrap_content
                                    scaleType = ImageView.ScaleType.FIT_XY
                                    adjustViewBounds = true
                                    margin_bottom = 6
                                    showImage(image.url)
                                }
                            }
                        }
                    }
                    contentText.text = response.data?.first?.ovoqe3wroqadzqso.parseEmpty()
                }
            })
    }

}